<%#
 Copyright 2013-2020 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
import { element, by, ElementFinder, ElementArrayFinder } from 'protractor';

import {
  waitUntilAnyDisplayed<% if (!readOnly) { %>,
  waitUntilDisplayed,
  click,
  waitUntilHidden,
  isVisible,<% } %>
} from '../../<%= entityParentPathAddition %>util/utils';

import NavBarPage from './../../<%= entityParentPathAddition %>page-objects/navbar-page';

<%_ if (!readOnly) { _%>
import <%= entityClass %>UpdatePage from './<%= entityFileName %>-update.page-object';

const expect = chai.expect;
<%_ } _%>
<%_ if (!readOnly) { _%>
export class <%= entityClass %>DeleteDialog {
    deleteModal = element(by.className('modal'));
    private dialogTitle: ElementFinder = element(by.id('<%= i18nKeyPrefix %>.delete.question'));
    private confirmButton = element(by.id('<%= jhiPrefixDashed %>-confirm-delete-<%= entityInstance %>'));

    getDialogTitle() {
        return this.dialogTitle;
    }

    async clickOnConfirmButton() {
        await this.confirmButton.click();
    }
}
<%_ } _%>


export default class <%= entityClass %>ComponentsPage {
    <%_ if (!readOnly) { _%>
    createButton: ElementFinder = element(by.id('jh-create-entity'));
    deleteButtons = element.all(by.css('div table .btn-danger'));
    <%_ } _%>
    title: ElementFinder = element(by.id('<%= entityFileName %>-heading'));
    noRecords: ElementFinder = element(by.css('#app-view-container .table-responsive div.alert.alert-warning'));
    table: ElementFinder = element(by.css('#app-view-container div.table-responsive > table'));

    records: ElementArrayFinder = this.table.all(by.css('tbody tr'));

    getDetailsButton(record: ElementFinder) {
      return record.element(by.css('a.btn.btn-info.btn-sm'));
    }

    <%_ if (!readOnly) { _%>
    getEditButton(record: ElementFinder) {
      return record.element(by.css('a.btn.btn-primary.btn-sm'));
    }
  
    getDeleteButton(record: ElementFinder) {
      return record.element(by.css('a.btn.btn-danger.btn-sm'));
    }
    <%_ } _%>

    async goToPage(navBarPage: NavBarPage) {
      await navBarPage.getEntityPage('<%= entityStateName %>');
      await waitUntilAnyDisplayed([this.noRecords, this.table]);
      return this;
    }

    <%_ if (!readOnly) { _%>
    async goToCreate<%= entityClass %>() {
      await this.createButton.click();
      return new <%= entityClass %>UpdatePage();
    }
  
    async delete<%= entityClass %>() {
      const deleteButton = this.getDeleteButton(this.records.last());
      await click(deleteButton);
  
      const <%= entityInstance %>DeleteDialog = new <%= entityClass %>DeleteDialog();
      await waitUntilDisplayed(<%= entityInstance %>DeleteDialog.deleteModal);
      expect(await <%= entityInstance %>DeleteDialog.getDialogTitle().getAttribute('id')).to.match(/<%= i18nKeyPrefix %>.delete.question/);
      await <%= entityInstance %>DeleteDialog.clickOnConfirmButton();

      await waitUntilHidden(<%= entityInstance %>DeleteDialog.deleteModal);

      expect(await isVisible(<%= entityInstance %>DeleteDialog.deleteModal)).to.be.false;
    }
    <%_ } _%>
}
